#include "W_CusWidget.h"
#include <QPainter>
#include <QPaintEvent>

W_CusWidget::W_CusWidget(QWidget *parent)
        : QWidget(parent)
{
    this->setWindowFlags(Qt::FramelessWindowHint);
    this->setAttribute(Qt::WA_TranslucentBackground, true);

    //初始化参数
    _plineColor = QColor("#49d9fe");
    _prectColor = QColor("#164381");
    _pshadowStartColor = QColor(69,105,154,20);
    _pshadowStopColor = QColor(69,105,154,0);
}

W_CusWidget::~W_CusWidget()
{
}

void W_CusWidget::drawBackground(const QRect & rect,QPainter * painter)
{
    QLinearGradient linearGradient(rect.topLeft(),QPoint(shadowLength,0));
    linearGradient.setColorAt(0.4,_pshadowStartColor);
    linearGradient.setColorAt(0.8,_pshadowStopColor);
    painter->fillRect(QRect(rect.topLeft(),QSize(shadowLength,rect.height())),linearGradient);

    linearGradient.setStart(rect.topRight());
    linearGradient.setFinalStop(QPoint(rect.width() - shadowLength,0));
    painter->fillRect(QRect(QPoint(rect.width() - shadowLength,0),QSize(shadowLength,rect.height())),linearGradient);

    linearGradient.setStart(rect.topLeft());
    linearGradient.setFinalStop(QPoint(0,shadowLength));
    painter->fillRect(QRect(rect.topLeft(),QSize(rect.width(),shadowLength)),linearGradient);

    linearGradient.setStart(QPoint(0,rect.height()));
    linearGradient.setFinalStop(QPoint(0,rect.height() - shadowLength));
    painter->fillRect(QRect(QPoint(0,rect.height() - shadowLength),QSize(rect.width(),shadowLength)),linearGradient);

    painter->save();
    QPen pen;
    pen.setColor(_prectColor);
    pen.setWidth(3);
    painter->setPen(pen);
    painter->drawRect(rect.adjusted(0,0,-1,-1));
    painter->restore();

    drawLines(rect,painter);
}

void W_CusWidget::paintEvent(QPaintEvent * event)
{
    QPainter painter(this);
    auto rect = event->rect();
    drawBackground(rect,&painter);

    QWidget::paintEvent(event);
}

void W_CusWidget::drawLines(const QRect & rect,QPainter *painter)
{
    painter->save();
    QPen pen;
    pen.setColor(_plineColor);
    pen.setWidth(5);
    painter->setPen(pen);
    QVector<QLine> vector;
    vector << QLine(rect.topLeft(),QPoint(0,lightLineLength));
    vector << QLine(rect.topLeft(),QPoint(lightLineLength,0));
    vector << QLine(rect.topRight(),QPoint(rect.topRight().x() - lightLineLength,0));
    vector << QLine(rect.topRight(),QPoint(rect.topRight().x(),lightLineLength));
    vector << QLine(rect.bottomLeft(),QPoint(0,rect.bottomLeft().y() - lightLineLength));
    vector << QLine(rect.bottomLeft(),QPoint(lightLineLength,rect.bottomLeft().y()));
    vector << QLine(rect.bottomRight(),QPoint(rect.bottomRight().x() - lightLineLength,rect.bottomRight().y()));
    vector << QLine(rect.bottomRight(),QPoint(rect.bottomRight().x(),rect.bottomRight().y() - lightLineLength));
    painter->drawLines(vector);
    painter->restore();
}
